<template>
	<uni-popup ref="inputDialog" type="dialog" :is-mask-click="false" :mask-click="false">
		<view class="update-back">
			<progress :percent="total" show-info stroke-width="3" font-size="12" />
		</view>
	</uni-popup>
</template>

<script>
	export default {
		data() {
			return {
				total: 0, // 当前下载进度
				code: '0', // 1 表示强制更新 0表示不强制更新
				downloadTask: null, // 下载对象
			}
		},
		onBackPress() {
			if(this.code == '1') {
				this.$refs.inputDialog.close()
				this.updateApp()
				return true
			}
		},
		onLoad(event) {
			if (event.code) this.code = event.code
			this.updateApp()
		},
		methods: {
			updateApp() {
				uni.showLoading({
					title: '加载中···'
				})
				let appVersion = ''
				uni.getSystemInfo({
					success: function(e) {
						appVersion = e.platform
					}
				})
				let _this = this
				this.$http.get('api/Home/GetAppVersion').then(update => {
					uni.hideLoading()
					if(!update.apkurl) {
						uni.showToast({
							title: '没有最新的版本',
							icon: 'none'
						})
						_this.code = '0'
						setTimeout(() => {
							uni.navigateBack({delta: 1})
						},2000)
						return
					}
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						//此处为访问后台是否有更新
						uni.showModal({
							title: `版本${update.versionname || ''}更新`,
							content: update.desmess || '1.优化部分问题' , //更新描述
							confirmText: '立即更新',
							cancelText: '稍后进行',
							success: sucRes => {
								if (sucRes.confirm) {
									//如果是安卓直接更新下载
									if (appVersion == 'android') {
										_this.$refs.inputDialog.open()
										_this.androidAppk(update)
										//如果是ios跳转到app store
									} else {
										uni.setStorageSync('app-version',update.versionname)
										//在App Store Connect中的App Store下的app信息，可找到appleId
										plus.runtime.launchApplication({action: `https://apps.apple.com/cn/app/ePumping/id1490574764`}, function(e) {});
									}
								} else {
									if(_this.code == '1') plus.runtime.quit()
									else uni.navigateBack({delta: 1})
								}
							}
						})
					})
				}).catch(err => {
					uni.hideLoading()
				})
			},
			androidAppk(update) {
				let _this = this
				_this.downloadTask = uni.downloadFile({
					//下载地址
					url: update.apkurl,
					success: data => {
						if (data.statusCode === 200) {
							plus.runtime.install(data.tempFilePath,{},function() {
								console.log('versionnameversionnameversionnameversionname',update)
								uni.setStorageSync('app-version',update.versionname)
							})
							_this.$refs.inputDialog.close()
							uni.navigateBack({
								delta: 1
							})
						}
					},
					fail: err => {
						_this.$refs.inputDialog.close()
						uni.showToast({
							title: '文件下载失败',
							icon: 'error'
						})
					}
				})
				_this.downloadTask.onProgressUpdate((res) => {
					_this.total = res.progress
				});
			}
		}
	}
</script>

<style lang="scss">
	.update-back {
		background-color: #fff;
		border-radius: 5px;
		width: 475rpx;
		padding: 0 20rpx;
		height: 50px;
		line-height: 50px;
	}
</style>
